package com.yztc.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.yztc.chunboapp.MainActivity;
import com.yztc.chunboapp.R;
import com.yztc.utils.OtherLogin;
import com.yztc.utils.ToastHelper;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.sharesdk.framework.Platform;

/**
 * Created by My on 2016/10/9.
 */
public class LoginActivity extends AppCompatActivity {

    //点击关闭登录界面
    private ImageButton mIBtnClose;
    //点击登录
    private Button mBtnLogin;
    private TextView tv_toRegister;
    //用户名和密码
    private EditText mETtUserName,mETPWD;
    //第三方登录
    private TextView mTVThirdParty;
    private Platform platform;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_login);
        initView();
        //设置监听
        setOnclickListener();
        toRegister();
    }
    //设置监听
    public void setOnclickListener() {
        //点击关闭登录界面
        mIBtnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              finish();
            }
        });

        /**
         * 用户登录验证
         */
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = mETtUserName.getText().toString();
                String passWord = mETPWD.getText().toString();
                BmobUser bmobUser = new BmobUser();
                bmobUser.setUsername(userName);
                bmobUser.setPassword(passWord);
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if(e==null){
                            ToastHelper.toast(getApplication(),"登录成功",R.mipmap.appreise_ok);
                            MainActivity.tag = true;
                            finish();
                        }else{

                            ToastHelper.toast(getApplication(),"登录失败，请检查用户名或密码输入是否有误",R.mipmap.appreise_close);
                        }
                    }
                });
            }
        });

        //第三方登录
        mTVThirdParty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OtherLogin.authorize(platform);
            }
        });
     }

    public void initView() {
        mIBtnClose = (ImageButton) findViewById(R.id.ibtn_close);
        tv_toRegister= (TextView) findViewById(R.id.tv_toregister);
        mETtUserName = (EditText) findViewById(R.id.et_userName);
        mETPWD = (EditText) findViewById(R.id.et_psw);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mTVThirdParty = (TextView) findViewById(R.id.tv_thirdParty);
    }

    //设置监听，跳转到注册页面
    public void toRegister(){
        tv_toRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}
